package _me {
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Focus3D;
	
	import flash.geom.Vector3D;

	public class MathCoreWillDele {
		public static  function getSourcePro(vSourceXml : XML) : Array {
			// 将XML转为数组对象
			var tempArray : Array = new Array;
			for (var i : int = 0; i < vSourceXml.children().length(); i++) {
				var z : Object = new Object;
				for (var j : int = 0; j < vSourceXml.child(i).children().length(); j++) {
					var vT : String = String(vSourceXml.child(i).child(j).name());
					z[vT] = vSourceXml.child(i).child(j);
				}
				tempArray[i] = z;
			}
			return tempArray;
		}

		public static function argbToHex(a : uint, r : uint, g : uint, b : uint) : uint {
			// 转换颜色
			var color : uint = a << 24 | r << 16 | g << 8 | b;
			return color;
		}

		public static  function _catch_cam(_Cam : Camera3D, _focus_3d : Focus3D) : void {
			// 根据捆定点，及距离算出镜头的坐标及角度。

			var view_angle_x : Number = -_focus_3d.angle_x;
			var view_angle_y : Number = _focus_3d.angle_y ;
			var rx : Number = 0;
			var ry : Number = 0;
			var rz : Number = _Cam.distance;

			var sin_y : Number = Math.sin(view_angle_y * Math.PI / 180);
			var cos_y : Number = Math.cos(view_angle_y * Math.PI / 180);
			var sin_x : Number = Math.sin(view_angle_x * Math.PI / 180);
			var cos_x : Number = Math.cos(view_angle_x * Math.PI / 180);

			var tmp_rz : Number = rz;
			rz = cos_x * tmp_rz - sin_x * ry;
			ry = sin_x * tmp_rz + cos_x * ry;
			tmp_rz = rz;
			rz = cos_y * tmp_rz - sin_y * rx;
			rx = sin_y * tmp_rz + cos_y * rx;

			_Cam.x = rx + _focus_3d.x;
			_Cam.y = ry + _focus_3d.y;
			_Cam.z = rz + _focus_3d.z;

			_Cam.angle_x = _focus_3d.angle_x;
			_Cam.angle_y = _Cam.angle_y + ( _focus_3d.angle_y - _Cam.angle_y) / 1;

			_Cam.sin_y = Math.sin((-_Cam.angle_y) * Math.PI / 180);
			_Cam.cos_y = Math.cos((-_Cam.angle_y) * Math.PI / 180);

			_Cam.sin_x = Math.sin((_Cam.angle_x) * Math.PI / 180);
			_Cam.cos_x = Math.cos((_Cam.angle_x) * Math.PI / 180);

			_Cam.cameraMatrix.identity();
			_Cam.cameraMatrix.prependScale(1, _Cam.fovw / _Cam.fovh, 1);
			_Cam.cameraMatrix.prependTranslation(0, 0, -_Cam.distance);
			_Cam.cameraMatrix.prependRotation(-_Cam.angle_x, Vector3D.X_AXIS);
			_Cam.cameraMatrix.prependRotation(-_Cam.angle_y, Vector3D.Y_AXIS);
			_Cam.cameraMatrix.prependTranslation(-_focus_3d.x, -_focus_3d.y, -_focus_3d.z);
		}

		public static  function _catch_hit_cam(_Cam : Camera3D, _focus_3d : Focus3D) : void {
			// 根据捆定点，及距离算出镜头的坐标及角度。

			var view_angle_x : Number = -_focus_3d.angle_x;
			var view_angle_y : Number = _focus_3d.angle_y ;
			var rx : Number = 0;
			var ry : Number = 0;
			var rz : Number = _Cam.distance;

			var sin_y : Number = Math.sin(view_angle_y * Math.PI / 180);
			var cos_y : Number = Math.cos(view_angle_y * Math.PI / 180);
			var sin_x : Number = Math.sin(view_angle_x * Math.PI / 180);
			var cos_x : Number = Math.cos(view_angle_x * Math.PI / 180);

			var num : Number = _Cam.distance;
			
			var testNum:Number = 0;
			var tmp_rz : Number = 0;
			var backY : Number =0;
			for(var i:int;i<10;i++){
				testNum = i/10*num;
				
				 tmp_rz  = testNum;
				rx = 0;
				ry = 0;
				rz = cos_x * tmp_rz - sin_x * ry;
				ry = sin_x * tmp_rz + cos_x * ry;
				tmp_rz = rz;
				rz = cos_y * tmp_rz - sin_y * rx;
				rx = sin_y * tmp_rz + cos_y * rx;
				
				_Cam.x = rx + _focus_3d.x;
				_Cam.y = ry + _focus_3d.y;
				_Cam.z = rz + _focus_3d.z;
				
				 backY  = getNextPostionY(_Cam.x, _Cam.z);
				if (backY > _Cam.y) {
					num = testNum;
					break;
				}
				
			}
			
			while (true) {
				 tmp_rz  = num;
				rx = 0;
				ry = 0;
				rz = cos_x * tmp_rz - sin_x * ry;
				ry = sin_x * tmp_rz + cos_x * ry;
				tmp_rz = rz;
				rz = cos_y * tmp_rz - sin_y * rx;
				rx = sin_y * tmp_rz + cos_y * rx;

				_Cam.x = rx + _focus_3d.x;
				_Cam.y = ry + _focus_3d.y;
				_Cam.z = rz + _focus_3d.z;

				 backY  = getNextPostionY(_Cam.x, _Cam.z);
				if (num < 2 || backY < _Cam.y) {
					num = num - 10;
					break;
				}
				
				num--;
			}
			//num = _Cam.distance;
			//num=_Cam.distance+(num-_Cam.distance)/3;
			// _Cam.toString();

			_Cam.angle_x = _focus_3d.angle_x;
			_Cam.angle_y = _Cam.angle_y + ( _focus_3d.angle_y - _Cam.angle_y) / 1;

			_Cam.sin_y = Math.sin((-_Cam.angle_y) * Math.PI / 180);
			_Cam.cos_y = Math.cos((-_Cam.angle_y) * Math.PI / 180);

			_Cam.sin_x = Math.sin((_Cam.angle_x) * Math.PI / 180);
			_Cam.cos_x = Math.cos((_Cam.angle_x) * Math.PI / 180);

			_Cam.cameraMatrix.identity();
			_Cam.cameraMatrix.prependScale(1, _Cam.fovw / _Cam.fovh, 1);
			_Cam.cameraMatrix.prependTranslation(0, 0, -num);
			_Cam.cameraMatrix.prependRotation(-_Cam.angle_x, Vector3D.X_AXIS);
			_Cam.cameraMatrix.prependRotation(-_Cam.angle_y, Vector3D.Y_AXIS);
			_Cam.cameraMatrix.prependTranslation(-_focus_3d.x, -_focus_3d.y, -_focus_3d.z);
		}
        
		public static function getNextPostionY(_x : Number, _z : Number) : Number {
			_x = _x - Scene_data.laseScanningPesition.x;
			_z = _z - Scene_data.laseScanningPesition.z;
			var xx : int = (1024 / 2) + _x / 1;
			var yy : int = (1024 / 2) + _z / 1;

			var color32 : uint = Scene_data.groundBitmapData.getPixel(xx, yy);
			var red : int = color32 >> 16;
			var green : int = color32 >> 8 & 0xFF;
			var blue : int = color32 & 0xFF;
			var h : Number = green * 256 + red;
			if(color32==0||blue!=0){
				return 9999;
			}else{
				return Scene_data.scanningHight - h;
			}
		}
		public static function getNextPostionCanWalk(_x : Number, _z : Number,fy:Number) : Boolean {
			_x = _x - Scene_data.laseScanningPesition.x;
			_z = _z - Scene_data.laseScanningPesition.z;
			var xx : int = (1024 / 2) + _x / 1;
			var yy : int = (1024 / 2) + _z / 1;

			var color32 : uint = Scene_data.groundBitmapData.getPixel(xx, yy);
			var red : int = color32 >> 16;
			var green : int = color32 >> 8 & 0xFF;
		//	var blue : int = color32 & 0xFF;
			var h : Number = green * 256 + red;
			if(color32==0){
				return false;
			}else{
				return(Math.abs(fy-(Scene_data.scanningHight-h))<25);
			//	return true;
			}
		}
	}
}